package mobi.greenplate.theweb.web;

import java.io.Serializable;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;

import org.deltaset.faces.FacesUtils;
import org.deltaset.faces.PageHelper;

import mobi.greenplate.theweb.domain.Transport;
import mobi.greenplate.theweb.repository.TransportRepository;

//for : enum types (and many-to-one)

import mobi.greenplate.theweb.domain.TransportType;    
import mobi.greenplate.theweb.user.UserUtils;


@ManagedBean("transportBean")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class TransportBean implements Serializable {

    private static final String OUTCOME_DETAIL = "/secured/transport/detail?faces-redirect=true&transportId=%s";
    private static final String OUTCOME_LIST = "/secured/transport/list?faces-redirect=true";
    private static final long serialVersionUID = 1L;


    @Inject
    public TransportBean(final TransportRepository modelRepository) {
        this.modelRepository = modelRepository;
    }
    private final TransportRepository modelRepository;
	
	
	public Transport loadModel(String id) {
        if (null != id && !"".equals(id.trim())) {
            return modelRepository.findById(Long.valueOf(id));
        }
        return new Transport();
    }

    public boolean isNew(Transport model) {
        return null == model.getId();
    }

    public String delete(Transport model) {
        modelRepository.delete(model.getId());
        return OUTCOME_LIST;
    }
    
   public void deleteAndReset(Transport model, PageHelper<Transport> pageHelper) {
        delete(model);
        pageHelper.reset();
    }

    public String save(Transport model) {
        final Transport saved = modelRepository.save(model,UserUtils.currentUser());
        return String.format(OUTCOME_DETAIL, saved.getId().toString());
    }

    //always here. actually provides support for one-to-Many
    public void setModelTo(Transport model, String valueExpression){
        FacesUtils.setValueExpression(valueExpression,model);
    }
	

    public TransportType[] getTypeValues() {
        return TransportType.values();
    }


/// List handlers

    public PageHelper<Transport> all(String pageNum) {
        return all.load(pageNum);
    }
    private PageHelper<Transport> all = PageHelper.newInstance(new AllFinder());

    private static class AllFinder implements PageHelper.Finder<Transport> {

        private static final long serialVersionUID = 1L;

        public int counter(Object... params) {
            return FacesUtils.getManagedBean("transportRepository", TransportRepository.class).countAll();
        }

        public List<Transport> finder(int firstResult, int maxResult, Object... params) {
            return FacesUtils.getManagedBean("transportRepository", TransportRepository.class).
                    findAll(firstResult, maxResult);
        }
    }


    //current User (My)
    public PageHelper<Transport> my(String pageNum) {
        return my.load(pageNum, UserUtils.getUserId());
    }
    private PageHelper<Transport> my = PageHelper.newInstance(new ByOwnerFinder());

	//ManyToOne - by owner
    public PageHelper<Transport> byOwner(String pageNum, Long modelId) {
        return byOwner.load(pageNum, modelId);
    }
    private PageHelper<Transport> byOwner = PageHelper.newInstance(new ByOwnerFinder());

    private static class ByOwnerFinder implements PageHelper.Finder<Transport> {

        private static final long serialVersionUID = 1L;

        public int counter(Object... params) {
            return FacesUtils.getManagedBean("transportRepository", TransportRepository.class).countByOwner((Long) params[0]);
        }

        public List<Transport> finder(int firstResult, int maxResult, Object... params) {
            return FacesUtils.getManagedBean("transportRepository", TransportRepository.class).
                    findByOwner(firstResult, maxResult, (Long) params[0]);
        }
    }

	

    
}
